import { constantRoutes } from '../constantRoutes'
import { menuFieldInvert, dataToRouteMenu, routerToTree, treeToAscending, removeEmptyCatalog, resetRedirect } from './public'
import { cloneDeep } from 'lodash-es'

// 导出静态路由作为菜单数据使用
export const getConstantMenu = permitList => {
  const result: any = []
  if (constantRoutes.length > 0) {
    if (permitList && permitList.length > 0) {
      cloneDeep(constantRoutes).forEach(item => {
        if (!item.permit) {
          result.push(item)
        } else {
          const index = permitList.findIndex(v => v === item.permit)
          if (index >= 0) {
            result.push(item)
          }
        }
      })
    } else {
      cloneDeep(constantRoutes).forEach(item => {
        if (!item.permit) {
          result.push(item)
        }
      })
    }
  }
  return result
}

// 后端接口获取到的数据转化为菜单
export const asyncPermitMenu = (routeList, permitList) => {
  const result = getConstantMenu(permitList)
  const arr: any = []
  if (routeList && routeList.length > 0) {
    let routerArr = []
    if (result.length > 0) {
      routerArr = cloneDeep(menuFieldInvert(routeList)).concat(...result)
    } else {
      routerArr = cloneDeep(menuFieldInvert(routeList))
    }
    routerArr.forEach(item => {
      const itemRoute = dataToRouteMenu(item, false)
      arr.push(itemRoute)
    })
  } else {
    result.forEach(item => {
      const itemRoute = dataToRouteMenu(item, false)
      arr.push(itemRoute)
    })
  }
  if (arr.length > 0) {
    const treeRoutes = routerToTree(arr)
    // 数据排序
    treeToAscending(treeRoutes)
    // 过滤showLink不显示菜单
    const realMenu = filterMenuTree(treeRoutes)
    // 删除无子级菜单层路由的目录层菜单，这种目录层菜单不应该被显示
    removeEmptyCatalog(realMenu)
    // 重置各目录层的重定向
    resetRedirect(realMenu)
    return realMenu
  } else {
    return []
  }
}

// 过滤meta中showLink为false的路由
export const filterMenuTree = (data: any) => {
  const newTree = data.filter((v: any) => v.meta.showLink)
  newTree.forEach((v: any) => v.children && (v.children = filterMenuTree(v.children)))
  return newTree
}

// 获取当前菜单的根级菜单
export const getCurrentRootMenu = menu => {
  let rootPath = ''
  // 根据面包屑导航找到当前路由的根路由
  if (menu.meta.breadcrumb && (menu.meta.breadcrumb as any).length > 0) {
    rootPath = menu.meta.breadcrumb[0].path
  } else {
    rootPath = menu.path
  }
  return rootPath
}

// 获取当前菜单的根级菜单的所有子级菜单
export const getCurrentRootMenuChildren = (menuData, route) => {
  const rootPath = getCurrentRootMenu(route)
  let currentMenuList: any = []
  const recursionMenuData = menuData => {
    for (let i = 0; i < menuData.length; i++) {
      if (menuData[i].path === rootPath) {
        if (menuData[i].children && menuData[i].children.length > 0) {
          currentMenuList = menuData[i].children
        } else {
          // 如果当前路由没有子级数据，将自己返回显示在侧边栏
          currentMenuList = [menuData[i]]
        }
        // 使用for循环，提升代码性能，找到即终止循环
        break
      } else {
        if (menuData[i].children && menuData[i].children.length > 0) {
          recursionMenuData(menuData[i].children)
        }
      }
    }
  }
  recursionMenuData(menuData)
  return currentMenuList
}

// 获取当前菜单父级的所有子级
export const getCurrentMenuParentChildren = (menuData, menu) => {
  let currentMenuList = []
  const recursionMenuData = menuData => {
    for (let i = 0; i < menuData.length; i++) {
      if (menuData[i].meta.id === menu.meta.pid) {
        if (menuData[i].children && menuData[i].children.length > 0) {
          currentMenuList = menuData[i].children
        }
        break
      } else {
        if (menuData[i].children && menuData[i].children.length > 0) {
          recursionMenuData(menuData[i].children)
        }
      }
    }
  }
  recursionMenuData(menuData)
  return currentMenuList
}

// 判断当前根菜单是否有子级
export const hasChildrenMenuFn = (menuData, menu) => {
  const rootPath = getCurrentRootMenu(menu)
  let hasChildren = false
  for (let i = 0; i < menuData.length; i++) {
    if (menuData[i].path === rootPath) {
      if (menuData[i].children && menuData[i].children.length > 0) {
        hasChildren = true
      } else {
        hasChildren = false
      }
    }
  }
  return hasChildren
}
